<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <div></div>
    <div class="p-5 w-full auto flex justify-center">
      <div>
        <div>
          <!-- <input type="file" /> -->
          <Upload
            :file-list="fileList"
            :before-upload="beforeUpload"
            @remove="handleRemove"
            :maxCount="1"
            @change="handleChange"
          >
            <a-button>
              <UploadOutlined />
              选择文件
            </a-button>
          </Upload>
        </div>
        <div> 注意：<br />原有相同编码的表单将会被覆盖<br />不存在的基础数据将会被自动创建 </div>
      </div>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { UploadProps, Upload } from 'ant-design-vue';
  import { UploadOutlined } from '@ant-design/icons-vue';
  import { importSolution } from '/@/api/flowManage/flowManage';
  export default defineComponent({
    name: 'FormManagementFileModal',
    components: {
      BasicModal,
      Upload,
      UploadOutlined,
    },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const { createMessage } = useMessage();
      const isUpdate = ref(false);
      const dataRef = ref<Recordable>({});
      const fileList = ref<UploadProps['fileList']>([]);
      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        setModalProps({ confirmLoading: false, width: '30%' });
        fileList.value = [];
        try {
          dataRef.value = data?.record ?? {};
          isUpdate.value = data?.isUpdate;
        } catch (err) {
          console.log(err);
        }
      });

      const getTitle = '导入文件';

      //上传前校验
      const beforeUpload: UploadProps['beforeUpload'] = (file) => {
        fileList.value = [file];
        return false;
      };
      //上传change
      const handleChange = (e) => {
        console.log('e', e);
      };
      // 上传文件删除
      const handleRemove: UploadProps['onRemove'] = (file) => {
        const index = fileList.value.indexOf(file);
        const newFileList = fileList.value.slice();
        newFileList.splice(index, 1);
        fileList.value = newFileList;
      };

      async function handleSubmit() {
        try {
          setModalProps({ confirmLoading: true });

          let form = new FormData();
          if (fileList.value && fileList.value.length > 0) {
            fileList.value.forEach((file: UploadProps['fileList'][number], index) => {
              form.append(`att${index + 1}`, file as any);
            });

            await importSolution(form);
          } else {
            createMessage.warn('请选择文件');
            return;
          }

          createMessage.success('操作成功');
          closeModal();
          emit('success');
        } catch (err) {
          console.log(err);
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return {
        registerModal,
        getTitle,
        handleSubmit,
        beforeUpload,
        handleRemove,
        fileList,
        handleChange,
      };
    },
  });
</script>
